home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / PRINTING / PAGESET / PAGESET.ZIP / PageSetup.pas < prev    next >
Pascal/Delphi Source File  |  1996-09-12  |  9KB  |  308 lines

  1. (*****************************************
  2.  Sorry, no comments.
  3.  Any question, feel free to e-mail me :
  4.  Cezar Lenci
  5.  sintesis@dglnet.com.br
  6. ******************************************)
  7. unit PageSetup;
  8.  
  9. interface
  10.  
  11. Uses
  12.     Windows,Forms,CommDlg,Printers,SysUtils,Messages,Dialogs,Classes;
  13.  
  14. Type
  15.  
  16. TPageSetupFlags = (poDefaultMinMargins,poDisableMargins,poMargins,poMinMargins,
  17.                    poDisableOrientation,poDisablePaper,poDisablePrinter,
  18.                    poHundredthsOfMillimeters,poThousandthsOfInches);
  19. TPageOptions    = Set Of TPageSetupFlags;
  20.  
  21. TPageSetupDialog = class(TCommonDialog)
  22. private
  23.     FOptions : TPageOptions;
  24.     FFlags   : Longint;
  25.     FMarginLeft,
  26.     FMarginTop,
  27.     FMarginRight,
  28.     FMarginBottom,
  29.     FMinMarginLeft,
  30.     FMinMarginTop,
  31.     FMinMarginRight,
  32.     FMinMarginBottom : Integer;
  33.     FPaperLength,
  34.     FPaperWidth      : Short;
  35.     procedure SetOptions(Value : TPageOptions);
  36.     procedure SetLeft(Value : Integer);
  37.     procedure SetTop(Value : Integer);
  38.     procedure SetRight(Value : Integer);
  39.     procedure SetBottom(Value : Integer);
  40.     procedure SetMinLeft(Value : Integer);
  41.     procedure SetMinTop(Value : Integer);
  42.     procedure SetMinRight(Value : Integer);
  43.     procedure SetMinBottom(Value : Integer);
  44. public
  45.     constructor Create(AOwner : TComponent); override;
  46.     procedure Execute;
  47.     procedure GetDefaults;
  48. Published
  49.     Property Options : TPageOptions     Read FOptions         Write SetOptions;
  50.     Property MarginLeft      : Integer  Read FMarginLeft      Write SetLeft;
  51.     Property MarginTop       : Integer  Read FMarginTop       Write SetTop;
  52.     Property MarginRight     : Integer  Read FMarginRight     Write SetRight;
  53.     Property MarginBottom    : Integer  Read FMarginBottom    Write SetBottom;
  54.     Property MinMarginLeft   : Integer  Read FMinMarginLeft   Write SetMinLeft;
  55.     Property MinMarginTop    : Integer  Read FMinMarginTop    Write SetMinTop;
  56.     Property MinMarginRight  : Integer  Read FMinMarginRight  Write SetMinRight;
  57.     Property MinMarginBottom : Integer  Read FMinMarginBottom Write SetMinBottom;
  58.     Property PaperLength     : Short    Read FPaperLength;
  59.     Property PaperWidth      : Short    Read FPaperWidth;
  60. end;
  61.  
  62. Procedure Register;
  63.  
  64. implementation
  65.  
  66. procedure CenterWindow(Wnd: HWnd);
  67. var
  68.   Rect: TRect;
  69. begin
  70.   GetWindowRect(Wnd, Rect);
  71.   SetWindowPos(Wnd, 0,
  72.     (GetSystemMetrics(SM_CXSCREEN) - Rect.Right + Rect.Left) div 2,
  73.     (GetSystemMetrics(SM_CYSCREEN) - Rect.Bottom + Rect.Top) div 3,
  74.     0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOZORDER);
  75. end;
  76.  
  77. function DialogHook(Wnd: HWnd; Msg: UINT; WParam: WPARAM; LParam: LPARAM): UINT; stdcall;
  78. begin
  79.   Result := 0;
  80.   case Msg of
  81.     WM_INITDIALOG:
  82.       begin
  83.         CenterWindow(Wnd);
  84.         Result := 1;
  85.       end;
  86.   end;
  87. end;
  88.  
  89. function TaskModalDialog(DialogFunc: Pointer; var DialogData): Bool;
  90. type
  91.     TDialogFunc = function(var DialogData): Bool stdcall;
  92. var
  93.     ActiveWindow: HWnd;
  94.     WindowList: Pointer;
  95. begin
  96.     ActiveWindow := GetActiveWindow;
  97.     WindowList := DisableTaskWindows(0);
  98.     try
  99.         Result := TDialogFunc(DialogFunc)(DialogData);
  100.     finally
  101.         EnableTaskWindows(WindowList);
  102.         SetActiveWindow(ActiveWindow);
  103.     end;
  104. end;
  105.  
  106. procedure GetPrinter(var DeviceMode, DeviceNames: THandle);
  107. var
  108.   Device, Driver, Port: array[0..79] of char;
  109.   DevNames: PDevNames;
  110.   Offset: PChar;
  111. begin
  112.   Printer.GetPrinter(Device, Driver, Port, DeviceMode);
  113.   if DeviceMode <> 0 then
  114.   begin
  115.     DeviceNames := GlobalAlloc(GHND, SizeOf(TDevNames) +
  116.      StrLen(Device) + StrLen(Driver) + StrLen(Port) + 3);
  117.     DevNames := PDevNames(GlobalLock(DeviceNames));
  118.     try
  119.       Offset := PChar(DevNames) + SizeOf(TDevnames);
  120.       with DevNames^ do
  121.       begin
  122.         wDriverOffset := Longint(Offset) - Longint(DevNames);
  123.         Offset := StrECopy(Offset, Driver) + 1;
  124.         wDeviceOffset := Longint(Offset) - Longint(DevNames);
  125.         Offset := StrECopy(Offset, Device) + 1;
  126.         wOutputOffset := Longint(Offset) - Longint(DevNames);;
  127.         StrCopy(Offset, Port);
  128.       end;
  129.     finally
  130.       GlobalUnlock(DeviceNames);
  131.     end;
  132.   end;
  133. end;
  134.  
  135.  
  136. function CopyData(Handle: THandle): THandle;
  137. var
  138.   Src, Dest: PChar;
  139.   Size: Integer;
  140. begin
  141.   if Handle <> 0 then
  142.   begin
  143.     Size := GlobalSize(Handle);
  144.     Result := GlobalAlloc(GHND, Size);
  145.     if Result <> 0 then
  146.       try
  147.         Src := GlobalLock(Handle);
  148.         Dest := GlobalLock(Result);
  149.         if (Src <> nil) and (Dest <> nil) then Move(Src^, Dest^, Size);
  150.       finally
  151.         GlobalUnlock(Handle);
  152.         GlobalUnlock(Result);
  153.       end
  154.   end
  155.   else Result := 0;
  156. end;
  157.  
  158. constructor TPageSetupDialog.Create(AOwner : TComponent); 
  159. begin
  160.     inherited Create(AOwner);
  161.     FOptions := [poDefaultMinMargins,poHundredthsOfMillimeters];
  162. End;
  163.  
  164. procedure TPageSetupDialog.Execute;
  165. var
  166.   PageDlgRec: TPageSetupDlg;
  167.   DevHandle: THandle;
  168. begin
  169.   FillChar(PageDlgRec, SizeOf(PageDlgRec), 0);
  170.   with PageDlgRec do
  171.   begin
  172.     lStructSize := SizeOf(PageDlgRec);
  173.     hInstance   := System.HInstance;
  174.     GetPrinter(DevHandle,hDevNames);
  175.     hDevMode    := CopyData(DevHandle);
  176.     rtMargin    := Rect(MarginLeft,MarginTop,MarginRight,MarginBottom);
  177.     rtMinMargin := Rect(MinMarginLeft,MinMarginTop,MinMarginRight,MinMarginBottom);
  178.     Flags       := PSD_ENABLEPAGESETUPHOOK or FFlags;
  179.     hWndOwner   := Application.Handle;
  180.     lpfnPageSetupHook := DialogHook;
  181.   End;
  182.   TaskModalDialog(@PageSetupDlg, PageDlgRec);
  183.   with PageDlgRec do
  184.   begin
  185.     MarginLeft   := rtMargin.Left;
  186.     MarginTop    := rtMargin.Top;
  187.     MarginRight  := rtMargin.Right;
  188.     MarginBottom := rtMargin.Bottom;
  189.   End;
  190. end;
  191.  
  192. procedure TPageSetupDialog.GetDefaults;
  193. var
  194.   PageDlgRec: TPageSetupDlg;
  195.   DevHandle: THandle;
  196. begin
  197.   FillChar(PageDlgRec, SizeOf(PageDlgRec), 0);
  198.   with PageDlgRec do
  199.   begin
  200.     lStructSize := SizeOf(PageDlgRec);
  201.     hInstance   := System.HInstance;
  202.     GetPrinter(DevHandle,hDevNames);
  203.     rtMargin    := Rect(MarginLeft,MarginTop,MarginRight,MarginBottom);
  204.     rtMinMargin := Rect(MinMarginLeft,MinMarginTop,MinMarginRight,MinMarginBottom);
  205.     Flags       := PSD_RETURNDEFAULT or PSD_ENABLEPAGESETUPHOOK or FFlags;
  206.     hWndOwner   := Application.Handle;
  207.     lpfnPageSetupHook := DialogHook;
  208.   End;
  209.   TaskModalDialog(@PageSetupDlg, PageDlgRec);
  210.   with PageDlgRec do
  211.   begin
  212.     MarginLeft   := rtMargin.Left;
  213.     MarginTop    := rtMargin.Top;
  214.     MarginRight  := rtMargin.Right;
  215.     MarginBottom := rtMargin.Bottom;
  216.   End;
  217. end;
  218.  
  219.  
  220. procedure TPageSetupDialog.SetOptions(Value : TPageOptions);
  221. Begin
  222.     If (poDefaultMinMargins in Value) And Not (poDefaultMinMargins in FOptions) Then
  223.         Value := Value - [poMinMargins];
  224.     If (poMinMargins in Value) And Not (poMinMargins in FOptions) Then
  225.         Value := Value - [poDefaultMinMargins];
  226.     If (poHundredthsOfMillimeters in Value) And Not (poHundredthsOfMillimeters in FOptions) Then
  227.         Value := Value - [poThousandthsOfInches];
  228.     If (poThousandthsOfInches in Value) And Not (poThousandthsOfInches in FOptions) Then
  229.         Value := Value - [poHundredthsOfMillimeters];
  230.     FOptions := Value;
  231.     FFlags := 0;
  232.     If poDefaultMinMargins in FOptions then
  233.         FFlags := FFlags or PSD_DEFAULTMINMARGINS;
  234.     If poDisableMargins in FOptions then
  235.         FFlags := FFlags or PSD_DISABLEMARGINS;
  236.     If poMargins in FOptions then
  237.         FFlags := FFlags or PSD_MARGINS;
  238.     If poMinMargins in FOptions then
  239.         FFlags := FFlags or PSD_MINMARGINS;
  240.     If poDisableOrientation in FOptions then
  241.         FFlags := FFlags or PSD_DISABLEORIENTATION;
  242.     If poDisablePaper in FOptions then
  243.         FFlags := FFlags or PSD_DISABLEPAPER;
  244.     If poDisablePrinter in FOptions then
  245.         FFlags := FFlags or PSD_DISABLEPRINTER;
  246.     If poHundredthsOfMillimeters in FOptions then
  247.         FFlags := FFlags or PSD_INHUNDREDTHSOFMILLIMETERS;
  248.     If poThousandthsOfInches in FOptions then
  249.         FFlags := FFlags or PSD_INTHOUSANDTHSOFINCHES;
  250. End;
  251.  
  252. procedure TPageSetupDialog.SetLeft(Value : Integer);
  253. Begin
  254.     If Value > FMinMarginLeft Then
  255.         FMarginLeft := Value;
  256. End;
  257.  
  258. procedure TPageSetupDialog.SetTop(Value : Integer);
  259. Begin
  260.     If Value >= FMinMarginTop Then
  261.         FMarginTop := Value;
  262. End;
  263.  
  264. procedure TPageSetupDialog.SetRight(Value : Integer);
  265. Begin
  266.     If Value >= FMinMarginRight Then
  267.         FMarginRight := Value;
  268. End;
  269.  
  270. procedure TPageSetupDialog.SetBottom (Value : Integer);
  271. Begin
  272.     If Value >= FMinMarginBottom Then
  273.         FMarginBottom := Value;
  274. End;
  275.  
  276. procedure TPageSetupDialog.SetMinLeft(Value : Integer);
  277. Begin
  278.     If Value <= FMarginLeft Then
  279.         FMinMarginLeft := Value;
  280. End;
  281.  
  282. procedure TPageSetupDialog.SetMinTop(Value : Integer);
  283. Begin
  284.     If Value <= FMarginTop Then
  285.         FMinMarginTop := Value;
  286. End;
  287.  
  288. procedure TPageSetupDialog.SetMinRight(Value : Integer);
  289. Begin
  290.     If Value <= FMarginRight Then
  291.         FMinMarginRight := Value;
  292. End;
  293.  
  294. procedure TPageSetupDialog.SetMinBottom (Value : Integer);
  295. Begin
  296.     If Value <= FMarginBottom Then
  297.         FMinMarginBottom := Value;
  298.     Self.GetDefaults;
  299. End;
  300.  
  301. Procedure Register;
  302. Begin
  303.     RegisterComponents('Dialogs',[TPageSetupDialog]);
  304. End;
  305.  
  306.  
  307. end.
  308.